(function()
{

    var getUrlVars = function(url)
	{
	    var vars = [], hash, i;
	    var hashes = url.slice(url.indexOf('?') + 1).split('&');
	    for(i = 0; i < hashes.length; i++)
	    {
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
	    return vars;
	}

    var scripts = document.getElementsByTagName('script');
	var scriptTag = scripts[scripts.length - 1];
    var src = new String(scriptTag.src);
	var result = src.match(/Tv_Arte_CacheableIFrame_([0-9a-z]+)_([a-z]{0,2})\.js/);
 	if (result != null && result.length == 3) { // version cachee
 	    var id = result[1],
			lang = result[2],
			ROOT_HTTP = 'http://www.arte.tv/header-include',
			url = ROOT_HTTP + '/static/' + id + '_' + (lang || 'fr') + '.html';
 	} else {
        var GET = getUrlVars(scriptTag.src);
	 	var ROOT_HTTP = scriptTag.src.split('?')[0].replace('/assets/js/Tv_Arte_CacheableIFrame.js', '');
	    var url = ROOT_HTTP + '/static/' + GET.id + '_' + (GET.lang || 'fr') + '.html';
 	}

	var iFrame = document.createElement('iframe');
	iFrame.setAttribute('src', url);
	iFrame.setAttribute('id', 'arte-header');
	iFrame.setAttribute('allowtransparency', 'true');
	iFrame.setAttribute('frameBorder', '0');
	iFrame.setAttribute('scrolling', 'no');
	document.body.insertBefore(iFrame, document.body.childNodes[0]);
    var cssLink = document.createElement('link');
	cssLink.setAttribute('type', 'text/css');
	cssLink.setAttribute('href', ROOT_HTTP + '/assets/css/render-iframe' + ((navigator.appName.toString().indexOf('Microsoft') != -1) ? '_ie' : '') +'.css');
	cssLink.setAttribute('rel', 'stylesheet');
	cssLink.setAttribute('media', 'all');
	document.getElementsByTagName('head')[0].appendChild(cssLink);

})();